package com.lagou.task02;

import java.util.Scanner;

public class FiveRowTest {


    public static void main(String[] args) {
        FiveRow fc = new FiveRow();
        //绘制棋盘
        String[][] board = fc.drawBoard();

        //创建两个玩家 并给定棋子
        Player p1 = new Player("玩家一","●");
        Player p2 = new Player("玩家二","○");

        int sequence = 1;   //游戏顺序  1代表轮到玩家一  2代表轮到玩家2
        int chessNum = 0;   //当前棋盘上的棋子数，如果棋盘已经没有位置了还没分出胜负，跳出循环。

        Scanner sc = new Scanner(System.in);

        System.out.println("游戏开始！落子请输入x，y坐标的整数(0-15)，用空格或者换行隔开。");
        while (true){
            System.out.println("------------------------------------------");

            //获取当前玩家对象
            Player currentPlayer = sequence == 1 ? p1 : p2;
            //提示玩家落子
            System.out.println("请" + currentPlayer.getName() + "落子");

            //由于棋盘是从第二行第二列开始算起 玩家输入的坐标要加1
            int x = sc.nextInt() + 1;
            int y = sc.nextInt() + 1;

            //判断该点是否可以落子 并进行播报
            boolean isSuccess = currentPlayer.chess(board, x, y);
            if (isSuccess){

                // 玩家执行落子，更新棋盘并打印
                fc.updateBoardAndPrint(board,x,y,currentPlayer);

                //总棋子个数+1
                chessNum++;

                //检查是否分出胜负
                boolean isWin = currentPlayer.checkSelfWin(board,x,y);
                if (isWin){
                    break;
                }
                if (chessNum >= 16 * 16){
                    System.out.println("棋盘已经没有位置，本局没有胜负。");
                    break;
                }
                sequence = sequence == 1 ? 2 : 1;
            }
        }
    }
}
